package com.example.sample;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mgp_sdk_example.R;
import com.weibo.game.sdk.WeiboGameSDKAPI;
import com.weibo.game.sdk.WeiboGameSDKHelper;
import com.weibo.game.sdk.callback.SinaGameCallBack;
import com.weibo.game.sdk.callback.SinaUser;

public class MainActivity extends Activity implements View.OnClickListener{
    public static final int START = 0;
    public static final int SUCCESS = 1;
    public static final int ERROR = 2;
    public static final int CANCEL = 3;
    public static final int TIMEOUT = 4;
    
    public static final String TAG = "DEBUG";
    
    private Activity act;
    private TextView tv_result;
    private EditText et_money;
    
    @Override
    public void onBackPressed() {
    	exit(this);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //初始化
        WeiboGameSDKAPI.init(this);
        super.onCreate(savedInstanceState);
        act = this;

        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button1a).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        et_money  =   (EditText) findViewById(R.id.button_money);
        tv_result = (TextView) findViewById(R.id.tv_result);
        
        //工具类
        String ip = WeiboGameSDKHelper.getIPAddress();
        String machineId = WeiboGameSDKHelper.getMachineId(this);
        Log.w(TAG, "ip地址： " + ip);
        Log.w(TAG, "machineId： " + machineId);
        tv_result.setText(android.os.Build.MODEL);
    }
    
    
    /**
     * 登出
     */
    public void logout(final Activity act){
        //登出
        WeiboGameSDKAPI.logout(act);
        
        //模拟流程，进入登入页面
        Toast.makeText(act, "模拟流程示例，3秒后调用登入函数，进入登入页面", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                login(act);
            }
        }, 3000);
    }
    
    /**
     * 登入
     * @param act
     */
    public void login(final Activity act){
        WeiboGameSDKAPI.login(act, new SinaGameCallBack() {
            @Override
            public void onResult(int code, Object obj) {
                switch (code) {
                //成功
                case SUCCESS:
                    SinaUser user = (SinaUser) obj;
                    Toast.makeText(act, "登入成功，用户的json信息："+ user.toString(), Toast.LENGTH_SHORT).show();
                    logResult(code, obj);
                    break;
                case CANCEL:
                    Toast.makeText(act, "用户取消登入", Toast.LENGTH_SHORT).show();
                    logResult(code, obj);
                    break;
                case START:
                case ERROR:
                    //如果需要可以加入相关统计信息,通常不处理
                	logResult(code, obj);
                default:
                    break;
                }
            }
        });
    }
    
    public void loginByWeibo(final Activity act){
        WeiboGameSDKAPI.login(act, new SinaGameCallBack() {
            @Override
            public void onResult(int code, Object obj) {
                switch (code) {
                //成功
                case SUCCESS:
                    SinaUser user = (SinaUser) obj;
                    logResult(code, obj);
                    Toast.makeText(act, "登入成功，用户的json信息："+ user.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case CANCEL:
                	logResult(code, obj);
                    Toast.makeText(act, "用户取消登入", Toast.LENGTH_SHORT).show();
                    break;
                case START:
                case ERROR:
                    //如果需要可以加入相关统计信息,通常不处理
                	logResult(code, obj);
                default:
                    break;
                }
            }
        });
    }
    
    //切换账号
    public void switchAccount(final Activity act){
        WeiboGameSDKAPI.switchAccount(act, new SinaGameCallBack() {
            @Override
            public void onResult(int code, Object obj) {
                switch (code) {
                //成功
                case SUCCESS:
                    SinaUser user = (SinaUser) obj;
                    logResult(code, obj);
                    Toast.makeText(act, "登入成功，用户的json信息："+ user.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case CANCEL:
                	logResult(code, obj);
                    Toast.makeText(act, "用户取消登入", Toast.LENGTH_SHORT).show();
                    break;
                case START:
                case ERROR:
                    //如果需要可以加入相关统计信息,通常不处理
                	logResult(code, obj);
                default:
                    break;
                }
            }
        });
    }
    
    /**
     * 支付
     * @param act
     * @param amount 金额，单位分
     * @param subject 商品主题
     * @param desc 商品描述
     * @param pt 透传参数，异步回调会回传给server，只用来做校验用，所以可以是任意值。
     */
    public void pay(final Activity act , int amount , String subject , String desc , String pt){
        WeiboGameSDKAPI.pay(act, amount, subject, desc, pt, new SinaGameCallBack() {
            @Override
            public void onResult(int code, Object obj) {
                switch (code) {
                case SUCCESS:
                	logResult(code, obj);
                    Toast.makeText(act, "支付成功,订单号:"+obj.toString() , Toast.LENGTH_SHORT).show();
                    break;
                case CANCEL:
                	logResult(code, obj);
                    Toast.makeText(act, "用户取消了支付" , Toast.LENGTH_SHORT).show();
                    break;
                case TIMEOUT:
                	logResult(code, obj);
                    Toast.makeText(act, "支付到账通知超时了，请以服务端异步通知为准" , Toast.LENGTH_SHORT).show();
                    break;
                case START:
                    Log.d(TAG, obj.toString());
                    break;
                case ERROR:
                	logResult(code, obj);
                    Toast.makeText(act, "ERROR:"+obj.toString() , Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
                }
            }
        });
    }
    
    /**
     * 退出
     * @param act
     */
    public void exit(final Activity act){
        WeiboGameSDKAPI.exit(act, new SinaGameCallBack() {
            @Override
            public void onResult(int code, Object obj) {
                switch (code) {
                case SUCCESS:
                	logResult(code, obj);
                    //退出程序
                	MainActivity.this.finish();
                    android.os.Process.killProcess(android.os.Process.myPid());
                    break;
                case CANCEL:
                	logResult(code, obj);
                    break;
                default:
                    break;
                }
            }
        });
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			login(act);
			break;
			
		case R.id.button1a:
			loginByWeibo(act);
			break;
			
		case R.id.button2:
            //判断是否登入过
            if(WeiboGameSDKAPI.isLogin(act)){
                String text = et_money.getText().toString().trim();
                if(!TextUtils.isEmpty(text)){
                    pay(act,Integer.valueOf(text), "支付1分钱测试标题", "支付1分钱测试描述", "透传参数，异步回调会回传给server");
                }
            }else{
                login(act);
            }
			break;
			
		case R.id.button3:
			exit(act);
			break;
			
		case R.id.button4:
			logout(MainActivity.this);
			break;
			
		case R.id.button5:
			switchAccount(MainActivity.this);
			break;

		default:
			break;
		}
	}
	
	private void logResult(int code, Object obj){
		String result = "code = " + code + ", msg: " + obj.toString();
		Log.d(TAG, result);
		tv_result.setText(result);
	}
}
